#include "bits/stdc++.h"
using namespace std;

int dataNum(vector<int>& m, vector<int>& n, int error_id, int data_input){

    vector<int> left(m.size());
    int first_send = data_input;
    for(int i = 0; i < m.size(); i++){
        if(error_id == i){
            left[i] = 0;
            continue;
        }
        int temp = min(first_send, m[i]);
        left[i] = max(first_send - temp, 0);
        left[i] = min(left[i], n[i]);
        first_send = temp;
    }

    int second_send = 0;
    for(int i = 0; i < m.size(); i++){
        if(error_id == i) continue;
        second_send = min(second_send + left[i], m[i]);
    }

    return first_send + second_send;
}

int main(int argc, char* argv[]){

    int k, a;
    cin >> k;

    vector<int> m(k);
    vector<int> n(k);
    vector<string> str(k);
    
    int idx=0;
    while(k--){
        cin>>str[idx];
        int left=0, right=0;
        while(right<str[idx].size()){
            if(str[idx][right] == ','){
                m[idx] = stoi(str[idx].substr(left, right-left));
                left = right+1;
            }
            if(right == str[idx].size()-1){
                n[idx] = stoi(str[idx].substr(left, right-left+1));
            }
            right++;
        }
        idx++;
    }
    cin >> a;

    int getData = dataNum(m, n, -1, a);

    for(int i=0; i<m.size(); i++){
        getData = min(dataNum(m, n, i, a), getData);
    }
    cout << getData << endl;

    return 0;
}